package com.iot.gateway.tsl.controller;

import com.iot.gateway.tsl.qo.SpecQo;
import com.iot.gateway.tsl.service.TslDeviceSpecService;
import com.iot.gateway.utils.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/device/spec")
@Tag(name = "物模型管理 - 协议扩展管理")
public class TslDeviceSpecController {

    private final TslDeviceSpecService specService;

    public TslDeviceSpecController(TslDeviceSpecService specService) {
        this.specService = specService;
    }

    @Operation(summary = "覆盖写入所有协议扩展", description = "一次性设置或覆盖指定设备的所有 specs")
    @PostMapping("/add/{deviceId}")
    public ApiResult<Void> setSpecs(
            @PathVariable String deviceId,
            @RequestBody Map<String, Object> specs) {
        specService.setSpecs(deviceId, specs);
        return ApiResult.success();
    }

    @Operation(summary = "更新单个协议扩展字段", description = "修改指定设备的某条协议扩展信息")
    @PostMapping("/update/{deviceId}/{key}")
    public ApiResult<Void> updateSpec(
            @PathVariable String deviceId,
            @PathVariable String key,
            @RequestBody SpecQo specQo) {
        specService.updateSpec(deviceId, key, specQo);
        return ApiResult.success();
    }

    @Operation(summary = "删除单个协议扩展字段", description = "删除指定设备的某条协议扩展信息")
    @DeleteMapping("/del/{deviceId}/{key}")
    public ApiResult<Void> deleteSpec(
            @PathVariable String deviceId,
            @PathVariable String key) {
        specService.deleteSpec(deviceId, key);
        return ApiResult.success();
    }

    @Operation(summary = "查询协议扩展列表", description = "获取指定设备的所有协议扩展信息")
    @GetMapping("/select/{deviceId}")
    public ApiResult<Map<String, Object>> getSpecs(@PathVariable String deviceId) {
        return ApiResult.success(specService.getSpecs(deviceId));
    }
}
